package com.puppy.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/*aop类名一般以Aspect结尾*/
@Slf4j
//@Aspect   //标记目前是aop类
@Component
public class RecordTimeAspect {

    @Around("execution(* com.puppy.service.impl.*.*(..))") //指定当前切面类要拦截的类和方法
    public Object recordTime(ProceedingJoinPoint pjp) throws Throwable {
        //1.记录方法运行的开始时间
        long begin = System.currentTimeMillis();

        //2.执行原始的方法
        Object result = pjp.proceed();

        //3.记录方法运行的结束时间
        long end = System.currentTimeMillis();
        log.info("方法{} 运行时间：{}ms", pjp.getSignature(), end - begin);
        return result;
    }
}
